<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/13
 * Time: 上午10:57
 */

namespace Api\Service;


class MoneyLogService extends BaseService
{
    protected $tmpl;
    protected $moneyLogModel;
    public function __construct()
    {
        $this->moneyLogModel=M('moneylog');
    }

    public function _money_msg($msg_user,$money,$mtype){

        //读取模板id并缓存
        $tmpl = S("TMPL");
        if(empty($tmpl) || 1){
            $tmpl = $this->_getTmpId();//获取模板id
            S("TMPL",$tmpl);
        }
        $this->tmpl = $tmpl;

        $tmp_id = $this->tmpl['YUE']['tmp_id'];
        $color = $this->tmpl['YUE']['color'];
        if($mtype==1) {
            $url = get_domain()."/mobile/moneylists/money";
            $left = $msg_user['money']+$msg_user['lock_money'];
            $name = C('YONGJIN_NAME');
        }
        if($mtype==2) {
            $url = get_domain()."/mobile/moneylists/score";
            $left = $msg_user['score'];
            $name = C('SCORE_NAME');
        }
        if($mtype==3) {
            $url = get_domain()."/mobile/moneylists/cash";
            $left = $msg_user['cash_money'];
            $name = C('CASH_NAME');
        }
        if($mtype==4) {
            $url = get_domain()."/mobile/moneylists/stock";
            $left = $msg_user['stock'];
            $name = '库存';
        }
        $header = $name.'消费:'.$money.'\\n';
        $footer ='\\n'."点击查看详情...";
        $msg = array(
            "touser" => $msg_user['openid'],
            "template_id" => $tmp_id,//
            'url' => $url,
            "topcolor" => $color,
            'data' => array(
                'first' => array(
                    'value' => urlencode($header),
                    'color' => $color
                ),
                'keyword1' => array(
                    'value' => urlencode($msg_user['nickname']),//昵称
                    'color' => $color,
                ),
                'keyword2' => array(
                    'value' => urlencode($left),//余额
                    'color' => $color,
                ),
                'keyword3' => array(
                    'value' => urlencode(date("Y-m-d H:i:s")),//时间
                    'color' => $color,
                ),
                'remark' => array(
                    'value' => urlencode($footer),
                    'color' => $color,
                ),
            ),
        );

        $msg_data = array(
            "uid" => $msg_user['uid'],
            "openid" => $msg_user['openid'],
            "nickname" => $msg_user['nickname'],
            "groupid" => -1,
            "content" => urldecode(json_encode($msg)),
            "send_priority" => 1,
            "create_time" => time(),
            "send_time" => time(),
            "status" => -1,
            "send_plan_time" => time()
        );
        $tmpmsgModel = new \Mobile\Model\tmpmsgModel;
        $tmpmsgModel->addtmpmsg($msg_data);//添加消息到数据库队列
        return true;
    }

    /**
     * 查询日志列表
     * @param $where
     * @param $page
     * @param int $pageSize
     * @param null $filed
     * @return mixed
     */
    public function getLogList($where,$page,$pageSize=10,$filed=null){
        if(!empty($filed)){
           return $this->moneyLogModel->field($filed)->where($where)->page($page,$pageSize)->order('log_id desc')->select();
        }else{
            return $this->moneyLogModel->where($where)->page($page,$pageSize)->order('log_id desc')->select();
        }
    }

    //条件查询总条数
    public function getCount($where){
        return $this->moneyLogModel->where($where)->count();
    }

    //统计金额
    public function sumCountMoney($where,$type='asc'){
        if($type=='asc'){
            $where['logtype']=1;//收入
            return $this->moneyLogModel->where($where)->sum('money');
        }else{
            $where['logtype']=0;//收入
            return $this->moneyLogModel->where($where)->sum('money');
        }
    }


}